{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一、from 10 number FDDB-Annonations file to a txt File "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "import shutil\n",
    "import os\n",
    "from xml.dom.minidom import Document    \n",
    "\n",
    "def widerFaceToRoidb():\n",
    "    faceCount = 0\n",
    "    boxCount = 0\n",
    "    outFile = open('test_fddb.txt','a')\n",
    "    for j in range(10):\n",
    "        if j < 9:\n",
    "            txtFileName = 'FDDB-folds/FDDB-fold-0%s.txt'%(str(j+1))\n",
    "        else:\n",
    "            txtFileName = 'FDDB-folds/FDDB-fold-10.txt'\n",
    "        fp = open(txtFileName,'r')\n",
    "        datas = fp.readlines()\n",
    "        for i in range(len(datas)):\n",
    "            data = datas[i].strip()\n",
    "            data = '/jmu/jmuaia.tanghaom.top/data/User/admin/home/train/faceDetector/data/FDDB/' + data + '.jpg'+'\\n'\n",
    "            outFile.write(data)\n",
    "    outFile.close()\n",
    "widerFaceToRoidb()\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二、 make FDDB dataset format from test result file. finishly.,make 10 txt file which like 10 train file' format."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "outPath = 'pyramid-59500'\n",
    "if not os.path.exists(outPath):\n",
    "        os.makedirs(outPath)\n",
    "fp = open('/jmu/jmuaia.tanghaom.top/data/User/admin/home/train/faceDetector/darknet-pyramid/results/Pyramid-59500-fddbface.txt','r')\n",
    "#fp = open('/jmu/jmuaia.tanghaom.top/data/User/admin/home/train/faceDetector/darknet-v1.0/results/35000-fddbface.txt','r')\n",
    "datas = fp.readlines()\n",
    "result = {}\n",
    "preImgName = datas[0].strip().split(' ')[0]\n",
    "preImgName = preImgName.split('.jpg')[0]\n",
    "preImgName = preImgName.replace('FDDB/','')\n",
    "print preImgName\n",
    "box =  datas[0].strip().split(' ')[1:]\n",
    "box[3] = str(float(box[3]) - float(box[1]))\n",
    "box[4] = str(float(box[4]) - float(box[2]))\n",
    "score = box[0]\n",
    "box[0:4] = box[1:5]\n",
    "box[4] = score\n",
    "print box\n",
    "result[preImgName] = [box]\n",
    "print(result)\n",
    "print box\n",
    "i = 1\n",
    "count = 1\n",
    "while i < len(datas):\n",
    "    imageName = datas[i].strip().split(' ')[0]\n",
    "    \n",
    "    imageName = imageName.split('.jpg')[0]\n",
    "    # 需要修改\n",
    "    imageName = imageName.replace('/jmu/jmuaia.tanghaom.top/data/User/admin/home/train/faceDetector/data/FDDB/','')\n",
    "    box =  datas[i].strip().split(' ')[1:]\n",
    "    box[3] = str(float(box[3]) - float(box[1]))\n",
    "    box[4] = str(float(box[4]) - float(box[2]))\n",
    "    score = box[0]\n",
    "    box[0:4] = box[1:5]\n",
    "    box[4] = score\n",
    "    #print box\n",
    "    if  imageName == preImgName:\n",
    "        result[imageName].append(box)\n",
    "    else: \n",
    "        count += 1\n",
    "        preImgName = imageName\n",
    "        result[imageName] = [box]\n",
    "    i+=1\n",
    "   \n",
    "\n",
    "fp.close()\n",
    "for k in range(10):\n",
    "    fileName = 'FDDB-folds/FDDB-fold-%s.txt'%(str(\"%02d\"%(k+1)))\n",
    "    outFile = open(fileName,'r')\n",
    "    fileData = outFile.readlines()\n",
    "    resultFile = open(outPath + '/fold-%s-out.txt'%(str(\"%02d\"%(k+1))),'a')\n",
    "    for data in fileData:\n",
    "        data = data.strip()\n",
    "        resultFile.write(data+'\\n')\n",
    "        imageName = data\n",
    "        boxes = []\n",
    "        if result.has_key(imageName):\n",
    "            boxes = result[imageName]\n",
    "        # print data,boxes\n",
    "        boxNumber = len(boxes)\n",
    "        resultFile.write(str(boxNumber)+'\\n')\n",
    "        for box in boxes:\n",
    "            lines = box[0] + ' ' + box[1] + ' ' + box[2] + ' '+ box[3] + ' '+box[4]+'\\n'\n",
    "            resultFile.write(lines)\n",
    "        if boxNumber == 0:\n",
    "            print data\n",
    "    resultFile.close()    \n",
    "    outFile.close()\n",
    "print(len(fileData))\n",
    "#print len(result['img_207'])\n",
    "print('image Number is :', count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三、 draw roc curve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mport numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from pylab import *\n",
    "import math\n",
    "x = []\n",
    "y = []\n",
    "file = ['CE-35000/DiscROC.txt','FL-28300/DiscROC.txt','pyramid-59500/DiscROC.txt','mtcnn-result/DiscROC.txt','HR-DiscROC.txt','FastCNNDisc.txt','DeepIRDiscROC(2).txt']\n",
    "labels = ['our CE-yolov3-spp-face','our FL-yolov3-spp-face','our-yolov3-spp-pyramid-face','mtcnn','CVPR2017 find tiny face','Fast RCNN','DeepIR']\n",
    "maxTPR = []\n",
    "for fileName in file:    \n",
    "    fp = open(fileName,'r')\n",
    "    datas= fp.readlines()\n",
    "    x_tmp = []\n",
    "    y_tmp = []\n",
    "    for data in datas:\n",
    "        data = data.strip().split(' ')\n",
    "        y_tmp.append(float(data[0]))\n",
    "        x_tmp.append(float(data[1]))\n",
    "    y.append(y_tmp)\n",
    "    x.append(x_tmp)\n",
    "    fp.close\n",
    "for i in range(len(x)):\n",
    "   maxTPR.append(max(y[i]))\n",
    "   plt.plot(np.array(x[i], dtype=int),np.array(y[i], dtype=float),label=labels[i] + '('+str(maxTPR[i])+')')\n",
    "   \n",
    "title='FDDB ROC Curve '\n",
    "plt.title(title)\n",
    "plt.xlabel('False Positive')\n",
    "plt.ylabel('True Positive Rate')\n",
    "plt.yticks(np.arange(0,1,0.05))\n",
    "plt.ylim([0.0, 1.0])\n",
    "plt.xlim([0.0, 2000.0])\n",
    "plt.grid(ls='-.')\n",
    "plt.legend()\n",
    "plt.savefig('ROC-curve.png', dpi=300)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
